#include<func.h>
//创建一个子线程，cancel子线程，在子线程中push两个清理函数，感受清理函数的执行顺序
void cleanup(void*p)//清理函数
{
   //free(p);
    printf("I am cleanup %ld\n",(long)p);
}

void* pthread_func(void *p)
{
    char *q=(char*)malloc(20);
    pthread_cleanup_push(cleanup,(void*)1);
    pthread_cleanup_push(cleanup,(void*)2);
    printf("malloc success\n");
    sleep(1);
    printf("start clean\n");
    pthread_cleanup_pop(1);
    printf("I am child thread\n");
    pthread_cleanup_pop(1);
    return NULL;
}

int main()
{
    pthread_t pthid;
    int ret=pthread_create(&pthid,NULL,pthread_func,NULL);
    THREAD_ERR_CHECK(ret,"pthread_creat");
    ret=pthread_cancel(pthid);//开始cancel时，子线程并没有开始free相应的空间
    THREAD_ERR_CHECK(ret,"pthread_cancel");
    ret=pthread_join(pthid,NULL);//等待子进程
    THREAD_ERR_CHECK(ret,"pthread_join");
    printf("I am main thread\n");
    return 0;
}
